Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
object-sizeof
Advanced tools
The object-sizeof npm package is used to calculate the approximate memory usage of JavaScript objects. It helps developers understand the memory footprint of their data structures, which can be crucial for optimizing performance and managing resources effectively.
Calculate size of a simple object
This feature allows you to calculate the memory size of a simple JavaScript object. The code sample demonstrates how to use the `sizeof` function to get the size of an object with basic key-value pairs.
const sizeof = require('object-sizeof');
const obj = { a: 1, b: 2, c: 3 };
console.log(sizeof(obj)); // Outputs the size of the object in bytes
Calculate size of a nested object
This feature allows you to calculate the memory size of a nested JavaScript object. The code sample shows how to use the `sizeof` function to get the size of an object with nested structures.
const sizeof = require('object-sizeof');
const nestedObj = { a: 1, b: { c: 2, d: { e: 3 } } };
console.log(sizeof(nestedObj)); // Outputs the size of the nested object in bytes
Calculate size of an array
This feature allows you to calculate the memory size of a JavaScript array. The code sample demonstrates how to use the `sizeof` function to get the size of an array.
const sizeof = require('object-sizeof');
const arr = [1, 2, 3, 4, 5];
console.log(sizeof(arr)); // Outputs the size of the array in bytes
Calculate size of a complex object
This feature allows you to calculate the memory size of a complex JavaScript object that includes various data types. The code sample shows how to use the `sizeof` function to get the size of an object with mixed data types.
const sizeof = require('object-sizeof');
const complexObj = { a: 1, b: [1, 2, 3], c: { d: 'string', e: true } };
console.log(sizeof(complexObj)); // Outputs the size of the complex object in bytes
The `sizeof` package provides similar functionality to `object-sizeof` by calculating the approximate memory usage of JavaScript objects. It is straightforward to use and offers a simple API for determining the size of various data structures.
The `object-size` package is another alternative that calculates the size of JavaScript objects. It provides a similar feature set to `object-sizeof` and can be used to measure the memory footprint of different data structures.
Node.js version uses the Buffer.from(objectToString) method to convert the object's string representation to a buffer, and then it uses the byteLength property to obtain the buffer size in bytes.
The module uses a combination of recursion and a stack to iterate through all of its properties, adding up the number of bytes for each data type it encounters.
Please note that this function will only work in some cases, especially when dealing with complex data structures or when the object contains functions.
Errors indicated by returned -1 in following cases:
It prevents potential exceptions or infinite loops, improving reliability.
The project follows JavaScript Standard Style as a JavaScript style guide. Code coverage reports are done using Codecov.io.
Code is written with the assumptions that any code added, which is not tested properly, is already or will be buggy. Hence test coverage, with the BDD style unit tests, stating the intent, and expected behaviour, is a must.
JavaScript does not provide sizeof (like in C), and programmer does not need to care about memory allocation/deallocation.
However, according to ECMAScript Language Specification, each String value is represented by 16-bit unsigned integer, Number uses the double-precision 64-bit format IEEE 754 values including the special "Not-a-Number" (NaN) values, positive infinity, and negative infinity.
Having this knowledge, the module calculates how much memory object will allocate.
npm install object-sizeof
// import sizeof from 'object-sizeof'
const sizeof = require('object-sizeof')
const sizeObj = sizeof({ abc: 'def' })
console.log(`Size of the object: ${sizeObj} bytes`)
const sizeInt = sizeof(12345)
console.log(`Size of the int: ${sizeInt} bytes`)
The MIT License (MIT)
Copyright (c) 2015, Andrei Karpushonak aka @miktam
FAQs
Sizeof of a JavaScript object in Bytes
We found that object-sizeof demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.